import user from '@/common/user.js'
import {setUserInfo} from '@/common/api.js'
let providerType={
	wx:'weixin',
	dy:'toutiao'
}
//登入
const Login=function(e){
	 uni.login({
		provider:providerType.wx,
		success: (res) => {
			console.log(res)
			uni.request({
				url: user.maiApiUrl+"/user/app/login",
				method:"POST",
				data: {
					code: res.code,
					app_type:providerType.wx,
					app_id: 1,
					invite_code:e
				},
				success(res) {
					if(res.data.code==0){
						user.state.token=res.data.data.token
						user.state.userId=res.data.data.user_id
						user.state.invite_code=res.data.data.invite_code
						user.state.city=res.data.data.city
						user.state.district=res.data.data.area
						user.state.latitude=res.data.data.latitude
						user.state.longitude=res.data.data.longitude
						//登入成功后写入缓存
						uni.setStorageSync("token",res.data.data.token)
						uni.setStorageSync("userId",res.data.data.user_id)
						uni.setStorageSync("inviteCode",res.data.data.invite_code)
						uni.setStorageSync("city",res.data.data.city)
						uni.setStorageSync("district",res.data.data.area)
						uni.setStorageSync("latitude",res.data.data.latitude)
						uni.setStorageSync("longitude",res.data.data.longitude)
						
					}else{
						uni.showToast({
							icon:"none",
							title:"登入失败!",
							duration:2000
						})
					}
				}
			})
		},
		fail: (error) => {
			console.log(error)
		}
	})
}
export default{
	Login
}